import Vue from 'vue'
import VueRouter from 'vue-router'
import Crud from '../views/curd.vue'
import Login from '../views/Login'
import Register from "../views/Register";
import { Message } from 'element-ui';
import AdminIndex from "../views/AdminIndex";
import TeaIndex from "../views/TeaIndex";
import StuIndex from "../views/StuIndex";
import DoHomework from "../views/DoHomework";
import HomeworkList from "../views/HomeworkList";

Vue.use(VueRouter)

const router =  new VueRouter({
  routes:[
    {
      name:'登录',
      path:'/login',
      component:Login,
      meta:{isRequired: false,title:'登录'}
    },
    {
      name:'注册',
      path:'/register',
      component:Register,
      meta:{isRequired: false,title:'注册'}
    },
    {
      name: '用户表',
      path: '/curd',
      component: Crud,
      meta: {isRequired: true, title: '用户列表'},
    },
    {
      name:'管理员',
      path:'/AdminIndex',
      component:AdminIndex,
      meta:{isAdmin: true,title:'管理员主页'}
    },
    {
      name:'教师端',
      path:'/TeaIndex',
      component: TeaIndex,
      meta: {isTea:true,title: "教师端主页"}
    },
    {
      name:'学生端',
      path: '/StuIndex',
      component: StuIndex,
      meta: {isStu:true,title: "学生端主页"}
    },
    {
      name:'作业列表',
      path: '/DoHomework',
      component: DoHomework,
    },
    {
      name:'作业布置',
      path: '/AddHomework',
      component: HomeworkList,
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.isRequired) {
    if (sessionStorage.loginName,sessionStorage.password) {
      next()
    } else {
      Message.warning({
        message: "你还未登录，请先登录"
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAdmin) {
    if (sessionStorage.getItem("status") === '0') {
      next()
    } else {
      Message.warning({
        message: "没有管理员账户无法进入"
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.isStu) {
    if (sessionStorage.getItem("status") === '2') {
      next()
    } else {
      Message.warning({
        message: "没有学生账户无法进入"
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.isTea) {
    if (sessionStorage.getItem("status") === '1') {
      next()
    } else {
      Message.warning({
        message: "没有老师账户无法进入"
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})

router.beforeEach((to, from, next) => {
  if (to.meta.isRequired) {
    if (sessionStorage.getItem("status") === '0') {
      next()
    } else {
      Message.warning({
        message: "没有管理员账户无法进入"
      })
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  } else {
    next()
  }
})


export default router
